package xin.ryven.car.android

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import xin.ryven.car.android.data.Music
import xin.ryven.car.android.databinding.ItemSongBinding
import xin.ryven.car.android.utils.FileUtils
import java.io.File

class SongAdapter(
    private val context: Context,
    private val onItemClick: (Int) -> Unit,
    private val onDownloadClick: (Music, Int) -> Unit
) : ListAdapter<Music, SongAdapter.ViewHolder>(SongDiffCallback()) {

    private val downloadProgress = mutableMapOf<Int, Int>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemSongBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        )
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(getItem(position), position)
    }

    fun updateDownloadProgress(position: Int, progress: Int) {
        downloadProgress[position] = progress
        notifyItemChanged(position)
    }

    inner class ViewHolder(
        private val binding: ItemSongBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        
        init {
            itemView.setOnClickListener {
                bindingAdapterPosition.takeIf { it != RecyclerView.NO_POSITION }?.let { position ->
                    val song = getItem(position)
                    if (FileUtils.isMusicFileExists(context, song)) {
                        onItemClick(position)
                    }
                }
            }
        }

        fun bind(music: Music, position: Int) {
            binding.songTitle.text = music.name
            binding.songArtist.text = music.singer

            if (FileUtils.isMusicFileExists(context, music)) {
                binding.btnDownload.isEnabled = false
                binding.btnDownload.text = "已下载"
                binding.downloadProgress.visibility = View.GONE
            } else {
                binding.btnDownload.isEnabled = true
                binding.btnDownload.text = "下载"
                
                val progress = downloadProgress[position] ?: 0
                if (progress in 1..99) {
                    binding.downloadProgress.visibility = View.VISIBLE
                    binding.downloadProgress.progress = progress
                } else {
                    binding.downloadProgress.visibility = View.GONE
                }
            }

            binding.btnDownload.setOnClickListener {
                onDownloadClick(music, position)
            }
        }
    }
}

class SongDiffCallback : DiffUtil.ItemCallback<Music>() {
    override fun areItemsTheSame(oldItem: Music, newItem: Music): Boolean {
        return oldItem.id == newItem.id
    }

    override fun areContentsTheSame(oldItem: Music, newItem: Music): Boolean {
        return oldItem == newItem
    }
} 